<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-02-20 09:56:45
 */

namespace app\admin\controller;
use app\common\controller\Backend;

class WeixinAccount extends Backend {

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '公众号管理', '', 'weixinAccount');

		$this->weixinAccountDao = model('weixin_manage/WeixinAccount');
	}

	public function index() {
		//获取列表
		$map = $parameter = array();

		$count = $this->weixinAccountDao->countSearch($map);
		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		$pageShow = $Page->show();
		list($list) = $this->weixinAccountDao->search($map, $Page->getLimit());
		//print_r($list);exit;
		$this->assign('count', $count);
		$this->assign('pageShow', $pageShow);
		$this->assign('list', $list);

		return $this->fetch();
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->weixinAccountDao->getInfo($id);
		}
		$this->assign('id', $id);
		$this->assign('info', $info);

		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if ($id) {
			$info = $this->weixinAccountDao->get($id);
		}

		if (!$info['t']) {
			$t = $this->get_token();
			$data['t'] = $t;
		}

		if (!$data['wxnumber']) {
			$this->error('请输入微信号');
		}

		list($code, $msg, $uploadInfo) = $this->comuploadFile('weixin', 'ewmurl', $info['ewmurl']);
		$uploadInfo['saveFullPath'] && $data['ewmurl'] = $uploadInfo['saveFullPath'];

		//验证数据
		//$this->validateData($data, 'ManageGroup.edit');

		if ($id) {
			$result = $this->weixinAccountDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->createCert($data);
				$this->success('保存成功', url('admin/weixinAccount/index'));
			} else {
				$this->error('保存失败'.showDbError($this->weixinAccountDao));
			}
		} else {
			$t = $this->get_token();
			$data['t'] = $t;
			$data['create_time'] = $this->nowtime;
			$result = $this->weixinAccountDao->baseAddData($data);
			if ($result !== false) {
				$this->createCert($data);
				$this->success('添加成功', url('admin/weixinAccount/index'));
			} else {
				$this->error('添加失败'.showDbError($this->weixinAccountDao));
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');

		$result = $this->weixinAccountDao->del($id);
		if ($result !== false) {
			$this->success('删除成功', url('admin/weixinAccount/index'));
		} else {
			$this->error('删除失败'.showDbError($this->weixinAccountDao));
		}
	}

	public function createCert($weixinAccountInfo) {
		$savePath = CMF_ROOT.'data/'.$weixinAccountInfo['wxnumber'].'/';
    	if (!is_dir($savePath)) {
	    	mkdir($savePath);
	    }
		$fp = fopen($savePath.'apiclient_cert.pem', "w");
		fwrite($fp, $weixinAccountInfo['apiclient_cert']);
		fclose($fp);
		$fp = fopen($savePath.'apiclient_key.pem', "w");
		fwrite($fp, $weixinAccountInfo['apiclient_key']);
		fclose($fp);
	}

	public function showApi() {
		$id = input('param.id', '', 'intval');
		$info = $this->weixinAccountDao->getInfo($id);

		$apiUrl = config('system.site_host')."/mobmall/weixin/index/t/".$info['t'];
		$this->assign('apiUrl', $apiUrl);

		echo $this->fetch()->getContent();
	}

	private function get_token($randLength=6,$attatime=1,$includenumber=0){
		if ($includenumber){
			$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789';
		}else {
			$chars='abcdefghijklmnopqrstuvwxyz';
		}
		$len=strlen($chars);
		$randStr='';
		for ($i=0;$i<$randLength;$i++){
			$randStr.=$chars[rand(0,$len-1)];
		}
		$tokenvalue=$randStr;
		if ($attatime){
			$tokenvalue=$randStr.time();
		}
		return $tokenvalue;
	}

}